import HitMouseMgr from "../HitMouseMgr";
import BaseView from "../../../cfw/mvc/BaseView";
import { EventName } from "../../../public/Config";
import SDKManager from "../../../sdk/SDKManager";
import BagManager from "../../bag/BagManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class HitMouseView extends BaseView {

	@property({ type: cc.Label, displayName: "score$VLabel" })
	score$VLabel: cc.Label = null;

	@property({ type: cc.Node, displayName: "content$VNode" })
	content$VNode: cc.Node = null;

	@property({ type: cc.Label, displayName: "time$VLabel" })
	time$VLabel: cc.Label = null;

	@property(cc.Prefab)
	prefab: cc.Prefab = null;

	@property(cc.Node)
	moneyPos: cc.Node = null;

	protected model: HitMouseMgr;
	protected time: number = 15;
	protected duration: number = 0;
	onLoad() {
		this.time = this.model.getTotalTime()
		console.log(' onLoad time == ', this.time)
		this.model.start()
		for (let index = 0; index < 9; index++) {
			let node = cc.instantiate(this.prefab)
			this.content$VNode.addChild(node)
		}
		this.eventProxy.on(EventName.UPDATE_CLICK_NUM, this.updateCount, this)
		this.updateCount()
	}

	update(dt: number) {
		if (!this.model.isOver()) {
			this.duration += dt;
			// console.log(' update dt== ', dt)
			if (this.duration >= 1) {
				this.duration = 0
				this.updateTime()
			}
		}
	}

	updateTime() {
		if (this.time >= 0) {
			// console.log(' updateTime time == ', this.time)
			this.time--;
			if (this.time == 1) {
				SDKManager.getChannel().showAppBoxAd(0)
			}
			if (this.time < 0) {
				this.model.setOverFlag(true)
				BagManager.addCoin(this.model.getHitCount() * 10,this.moneyPos.parent.convertToWorldSpaceAR(this.moneyPos.getPosition()),0.5)
				this.setTimeout(()=>{
					this.hide()
				},1000)
				
			} else {
				this.time$VLabel.string = '' + this.time
				// this.updateTime()
			}
		}
	}

	updateCount() {
		this.score$VLabel.string = '' + this.model.getHitCount()
	}




	onDestroy() {
		super.onDestroy()
	}



}